<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单编号"
          clearable
          @clear="getList"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="订单编号" prop="orderNum" align="center" width="160" fixed/>
      <el-table-column label="住户昵称" prop="customerName" align="center" width="120"/>
      <el-table-column label="园区" prop="area" align="center" width="120"/>
      <el-table-column label="订单金额" prop="amount" align="center" width="120">
        <template v-slot="{row}">
          <span>{{row.amount | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次余额" prop="lastMoney" align="center" width="120">
        <template v-slot="{row}">
          <span>{{row.lastMoney | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="本次余额" prop="nowMoney" align="center" width="120">
        <template v-slot="{row}">
          <span>{{row.nowMoney | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" prop="status" align="center" width="120">
        <template slot-scope="scope">
          <span   :style="{'color': colors(scope.row.status)}">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="质检状态" prop="orderStatus" align="center" width="120" >
        <template slot-scope="scope">
          <el-link @click="handleJumpExamine(scope.row)" :style="{'color': examineStatusColors(scope.row.qulityStatus),}">{{scope.row.qulityStatus}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="预约时间" prop="orderTime" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="extra" align="center" width="350" />
<!--      <el-table-column label="订单评价" prop="comment" align="center" width="350" />-->
      <el-table-column label="创建人" prop="createBy" width="120" align="center"/>
      <el-table-column label="修改人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <template v-if="scope.row.status!=='已退款'">
            <el-button size="mini" type="text" icon="el-icon-check" @click="sureOrder(scope.row)" v-if="scope.row.status!=='已送达'">确认</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(scope.row)" v-if="scope.row.type!=='buffet'">明细</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-if="scope.row.status!=='已送达'">修改</el-button>
            <el-button type="text"  icon="el-icon-download" size="mini" @click="rowExport(scope.row)">打印小票</el-button>
            <el-button type="text"  icon="el-icon-download" size="mini" @click="handleRefund(scope.row)" v-if="scope.row.status==='已送达'">退款</el-button>
          </template>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-if="scope.row.status!=='已送达'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改网上订单 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="订单金额" prop="amount" v-if="form.type==='buffet'">
          <el-input-number v-model="form.amount" :precision="2" :min="0" :controls="false" />
        </el-form-item>
        <el-form-item label="预约时间" prop="orderTime">
          <el-date-picker type="datetime" v-model="form.orderTime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                          :style="{ width: '100%' }" placeholder="请选择下单时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="订单备注" prop="extra">
          <el-input type="textarea" v-model="form.extra" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addOrder,
  list,
  listAll,
  del,
  get,
  update, orderRefund
} from "@/api/order/normal/online.js";
import {confirmOrder, getOrderList} from "@/api/order/panel";

export default {
  name: "Ordernormal",
  dicts: ["user_area"],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      orderType:'normal',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: '',
        orderType: "",
        area:'',
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
      },
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    handleJumpExamine(row) {
      if(row.examineStatus==='取消'){
        // this.$message.error("该质检单已取消");
        return
      }
      if (!row.qulityId) {
        this.$modal.msgWarning("无明细");
      } else {
        this.$router.push("/examine/goods/normal/detail/" + row.qulityId);
      }
    },
    colors(status) {
      if (status === "已送达") {
        return '#2a9c4a'
      }else if(status === "已退款"){
        return '#FF0000'
      } else  {
        return '#fe9f29'
      }
    },
    examineStatusColors(status) {
      if (status === "质检合格") {
        return '#2a9c4a'
      } else if (status === "质检不合格") {
        return '#FF0000'
      } else {
        return '#fe9f29'
      }
    },
    /** 查询列表 */
    getList() {
      this.queryParams.orderType=this.orderType
      getOrderList(this.queryParams).then((res) => {
        this.orderList = res.rows;
        this.total = res.total;
      }).finally(() => {

      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form = {
        status: "待确定"
      }
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    handleCommand(command, row) {
      switch (command) {
        case "handleDelete":
          this.handleDelete(row);
          break;
        case "handleDetail":
          this.handleDetail(row);
          break;
        case "handleDetail1":
          this.handleDetail1(row);
          break;
        case "handleInspection":
          this.handleInspection(row);
          break;
        default:
          break;
      }
    },
    // 跳转至点餐质检
    handleInspection(row) {

    },
    handleDetail: function(row) {
      const id = row.id;
      // this.$router.push("/order/list/detail/"+id);
      this.$router.push({
        path:'/order/list/detail',
        query:{
          id:id,
          orderType:this.orderType
        }
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder({...this.form, type: '点餐桌餐订单'}).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const roleIds = row.roleId || this.ids;/*  */
      this.$confirm('请确认是否删除该订单').then(res=>{
        del(row.id).then(res=>{
          this.$modal.msgSuccess("操作成功");
        }).finally(()=>{
          this.getList();
        })
      })
    },
    //订单确认
    sureOrder(row){
      this.$confirm('请确认是否完成该订单').then(res=>{
        row.status='已送达'
        row.orderType=row.type
        confirmOrder(row).then((response) => {
          this.$modal.msgSuccess("操作成功");
        }).finally(()=>{
          this.getList();
        })
      })
    },
    //行导出
    rowExport(row){
      if(row.type==='normal'){
        this.$router.push({
          path:'/jmreport/bookOrder',
          query:{
            id:'905707532635123712',
            rowId:row.orderNum,
            orderId:row.id
          }
        })
      }
      if(row.type==='replace'){
        this.$router.push({
          path:'/jmreport/bookOrder',
          query:{
            id:'1022806380436082688',
            rowId:row.orderNum,
            orderId:row.id
          }
        })
      }
    },
    //退款
    handleRefund(row){
      this.$confirm('请确认是否对该笔订单进行退款操作').then(res=>{
        orderRefund(row.id).then((response) => {
          this.$modal.msgSuccess("操作成功");
        }).finally(()=>{
          this.getList();
        })
      })
    },
  },
};
</script>
